package com.shuorikeji.huohuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.bean.MsgInfoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.utils.BusinessUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.BaseViewHolder> {
    private final Context mContext;
    private List<MsgInfoBean> mData = new ArrayList<>();
    private ClickCallBack callBack;

    public MsgAdapter(Context context, ClickCallBack callBackFlag) {
        this.mContext = context;
        this.callBack = callBackFlag;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.msg_item_layout, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvTitle.setText(mData.get(position).getTitle());
        try {
            holder.tvTime.setText(BusinessUtils.longTimeToDateStr(Long.parseLong(mData.get(position).getCreatetime()), "yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.tvContent.setText(Html.fromHtml(mData.get(position).getContent()));
    }

    public void setData(List<MsgInfoBean> data) {
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvContent;
        TextView tvTitle;

        public BaseViewHolder(View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }
}
